/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-6-9
 * Time: 上午10:30
 * To change this template use File | Settings | File Templates.
 */
Ext.define('cloud.controller.Admin', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            admin: 'admin',
            adminadd: 'adminadd',
            adminedit: 'adminedit',
            adminEdit_carid: 'adminedit #adminEdit_carid',
            admin_search: 'admin #admin_search',
            admin_btn_add: 'admin #admin_btn_add',
            admin_submit: 'adminadd #admin_submit',
            admin_edit: 'adminedit #admin_edit',
            adminSelect: 'admin #adminSelect'
        },
        control: {
            /**
             * search admin
             */
            admin_search: {
                change: function () {
                    var value = this.getAdmin_search().getValue();
                    var Store = Ext.getStore('ManagerAppFindStore');
                    Ext.getCmp('adminSelect').setStore(Store);
                    Store.clearFilter();
                    if (value != "") {
                        Store.filter('name', value, true);
                    }
                    Store.load({params: {caid: localStorage.getItem('caid')}});
                }
            },

            /**
             * show adminadd page
             */
            admin_btn_add: {
                tap: function () {
                    var adminAddView=this.getAdminadd() || Ext.create('cloud.view.admin.AdminAdd');
                    this.getAdmin().push(adminAddView);
                }
            },
            /**
             * show adminList
             */
            adminSelect: {
                itemtap: function (list, item, index, record, target, event) {
                    if (target.target.name == 'restpassword') {
                        Ext.Msg.confirm('确认重置', '', function (data) {
                            if (data == 'yes') {
                                Ext.Ajax.request({
                                    url: GLOBAL.api_url + 'manager_password_reset',
                                    scope: this,
                                    method: 'POST',
                                    params: {
                                        camid: localStorage.getItem('uid'),
                                        editid: record.get('camid'),
                                        caid: localStorage.getItem('caid')
                                    },
                                    success: function (response) {
                                        Ext.Viewport.unmask();
                                        Ext.Msg.alert(Ext.decode(response.responseText).Variables.Message.messagestr)
                                    },
                                    failure: function () {//请求失败时执行操作
                                        Ext.Viewport.unmask();
                                        Ext.Msg.alert('请求失败', '服务器维护中....');
                                    }
                                });
                            }
                        }, this);
                    } else {
                        Ext.Viewport.mask({xtype: 'loadmask'});
                        Ext.getStore('RoleAllFind').load({params: {caid: localStorage.getItem('caid')}});
                        var adminedit = this.getAdminedit()||Ext.create('cloud.view.admin.AdminEdit');
                        record.set('password', '');
                        adminedit.setRecord(record);
                        Ext.Viewport.unmask();
                        var itemIndex=record.get('camid') != localStorage.getItem('uid')?2:0;
                        Ext.getCmp('tabpanelEdit').remove(Ext.getCmp('tabpanelEdit').getItems().items[itemIndex], true);
                        this.getAdmin().push(adminedit);
                    }
                }
            },
            /**
             * del admin_image
             */
            admin_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getAdminedit().submit({
                                url: GLOBAL.api_url + 'admin_delete',
                                scope: this,
                                method: 'POST',
                                params: {
                                    //传递参数
                                    cdcid: Ext.getCmp('cdcid').getValue()
                                },
                                success: this.exesucess
                            });
                        }
                    }, this);
                }
            },
            /**
             * submit admin
             */
            admin_submit: {
                tap: function (list, item, index, record, target, event) {
                    if (Ext.isEmpty(Ext.getCmp('adminadd_name').getValue()))
                        return Ext.Msg.alert('请输入用户名');
                    if (Ext.isEmpty(Ext.getCmp('adminadd_password').getValue()))
                        return Ext.Msg.alert('请输入密码');
                    if (Ext.getCmp('adminadd_password').getValue().length < 3)
                        return Ext.Msg.alert('密码长度至少是三位');
                    if (Ext.getCmp('adminadd_password').getValue() != Ext.getCmp('adminadd_password_sure').getValue()) {
                        return Ext.Msg.alert('两次密码输入不一致');
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getAdminadd().submit({
                        url: GLOBAL.api_url + 'manager_add',
                        params: {
                            carid: Ext.getCmp('adminadd_carid').getValue(),
                            caid: localStorage.getItem('caid')
                        },
                        scope: this,
                        method: 'POST',
                        success: this.exesucess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * edit admin
             */
            admin_edit: {
                tap: function () {
                    switch (Ext.getCmp('tabpanelEdit').getActiveItem().id) {
                        case 'fieldset0':
                            this.getAdminedit().submit({
                                url: GLOBAL.api_url + 'manager_status_update',
                                params: {
                                    carid: Ext.getCmp('adminEdit_carid').getValue(),
                                    caid: localStorage.getItem('caid')
                                },
                                method: 'POST',
                                scope: this,
                                success: this.exesucess
                            });
                            break;
                        case 'fieldset1':
                            if (Ext.isEmpty(Ext.getCmp('adminedit_oldpassword').getValue()))
                                return Ext.Msg.alert('请输入旧密码');
                            if (Ext.isEmpty(Ext.getCmp('adminedit_password').getValue()))
                                return Ext.Msg.alert('请输入新密码');
                            Ext.Viewport.mask({xtype: 'loadmask'});
                            if (Ext.isEmpty(Ext.getCmp('adminedit_password').getValue()))
                                return Ext.Msg.alert('请输入密码');
                            if (Ext.getCmp('adminedit_password').getValue().length < 3)
                                return Ext.Msg.alert('密码长度至少是三位');

                            this.getAdminedit().submit({
                                url: GLOBAL.api_url + 'manager_password_update',
                                method: 'POST',
                                scope: this,
                                params: {
                                    camid: localStorage.getItem('uid')
                                },
                                success: this.exesucess
                            });
                            break;
                    }
                    Ext.Viewport.unmask();
                }
            }
        }
    },
    exesucess: function (from, result) {
        Ext.Msg.alert(result.Variables.Message.messagestr);
        if (result.Variables.Result.code == 0) {
            var Store = Ext.getStore('ManagerAppFindStore');
            Ext.getCmp('adminSelect').setStore('ManagerAppFindStore')
            Store.clearFilter();
            Store.load({params: {caid: localStorage.getItem('caid')}});
            this.getAdmin().pop();
        }
    }
})
;
